<!-- html -->
<template>
  <!-- <el-dialog :title="title" v-model="open" width="500px" append-to-body>
    <el-form ref="categoryRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="商品类别" prop="name">
        <el-input v-model="form.name" placeholder="请输入商品类别" />
      </el-form-item>
      <el-form-item label="所属分类" prop="parentId">
        <el-select v-model="form.parentId" clearable placeholder="请输入所属分类" style="width: 380px">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </div>
    </template>
  </el-dialog> -->
  <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="categoryRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="所属分类" prop="parentId">
          <el-tree-select
            v-model="form.parentId"
            :data="categoryOptions"
            :props="{ value: 'id', label: 'name', children: 'children' }"
            value-key="id"
            placeholder="请选择所属分类"
            check-strictly
          />
          
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
</template>

<!-- 
      ts
      setup 支持setup语法糖
 -->
<script setup>
import { reactive, ref, toRefs, defineEmits, getCurrentInstance, watch } from 'vue'

//获取当前组件的实例对象
const proxy = getCurrentInstance();


//接收父组件传过来的事件
const emits = defineEmits(['cancelBox', 'submitFormBox']);

//接收父组件传入的参数
let props = defineProps({
  title: {
    type: String,
    default: '新增分类'
  },
  open: {
    type: Boolean,
    default: false
  },
  formInfo: {
    type: Object,
    default: null
  },
  options: {
    type: Array,
    default: null
  },
  categoryOptions: {
    type: Array,
    default: null
  }
});
reactive
//抽离父组件传入的参数，解析成要的属性
let { title, open } = toRefs(props);

//定义表单参数
let form = reactive({
  id: null,
  name: null,
  parentId: null,
  isDelete: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null
});

let rules = reactive({
  name: [
    { required: true, message: "商品类别不能为空", trigger: "blur" }
  ],
  /* sort: [
    { required: true, message: "排序不能为空", trigger: "blur" }
  ],
  isDel: [
    { required: true, message: "删除标识不能为空", trigger: "blur" }
  ],
  createBy: [
    { required: true, message: "创建人不能为空", trigger: "blur" }
  ],
  createTime: [
    { required: true, message: "创建时间不能为空", trigger: "blur" }
  ] */
});

//定义函数-提交表单
const submitForm = () => {
  //校验表单

  proxy.refs.categoryRef.validate(valid => {
    //执行保存
    if (valid) {
      emits('submitFormBox', form)
    }
  })
};

//监听器
watch(() => props.formInfo, (newValue, oldValue) => {
  //深克隆
  form = reactive(JSON.parse(JSON.stringify(props.formInfo)))
}, { deep: true });

//定义函数-取消表单
const cancel = () => {
  //调用父组件方法
  emits('cancelBox')
};

</script>

<!-- css样式 -->
<style scoped></style>